(function ($) {
	var wardID = '', wardDesc = '',nurseid = ''
	var TomcatUrl = JSON.parse(localStorage.getItem('$user')).TomcatUrl;
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function getListData(){
		mui('#UL').off('tap','.mui-icon-phone',dialPhone)
		mui('#UL .photo img').each(function (index,element) {
			element.removeEventListener('error',photoLoadError)
		})
		var pdict = {'ward':wardID,"start":0,"limit":''}
		var paramDict = {'method':'web.INMHRComm:FindWardNurseList','params':JSON.stringify(pdict),'type':'RecQuery'}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result.msgcode != '1') {
				showNullTip(result.msg)
				return
			}
			updateUI(result['data']);
		}, function(errorStr) {
			showNullTip(errorStr)
		});
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var perInfo = app.getState()
		nurseid = perInfo['loginID']
		wardID = perInfo['loginWardId']
		wardDesc = perInfo['loginWardDesc'];
		if (wardID == undefined || wardID == '') {
			showNullTip('暂无数据')
			getAllWards()
		}else{
			getListData()
		}
		
		mui('body').on('tap','.otherPart',function(){
		  var webview = plus.webview.create('contactsOther.html', 'contactsOther', {
		  	background: '#fff',
		  	statusbar: {
		  		background: '#fff'
		  	}
		  },{'wardID':wardID});
		  webview.show('fade-in');
		}) 
	}
	function updateUI(dataArr){
		var innerHtml = ''
		var count = dataArr.length
		for (var i = 0; i < count;i++) {
			var dict = dataArr[i]
			var name = dict['PerName']
			var photo = dict['PerPhoto']||""
			name = name.split('(')[0]
			if (name.length > 2) {
				name = name.substring(name.length-2,name.length)
			}
			var phone = dict['PerPhone']||""
			phone = (phone==""?'暂无':phone)
			var isHead = dict['IsHead']=="Y"?'<div class="nurHead">护士长</div>':''
			innerHtml += '<li class="mui-table-view-cell">' +
				'<div class="photo" data-name="'+name+'" '+(photo==""?"":'style="background-color: transparent;border: solid 1px #75AC47;"')+'>' + 
					(photo==""?name:('<img src="'+TomcatUrl + '/Fileupdate/' + photo +'">')) + '</div>' +
				'<div class="nurName">' +
					'<div class="name"><div>' + dict['PerName'] + '</div>' + isHead + '</div>' +
					'<div class="phone">' + phone + ' </div>' +
				'</div>' +
				'<div class="mui-icon mui-icon-phone"></div></li>'
		}
		$('#UL')[0].innerHTML = '<li class="mui-table-view-cell">'+wardDesc+'<span>（'+count+'人）</span></li>'+innerHtml;
		if (count == 0) {
			showNullTip('暂无数据')
		}else{
			hideNullTip()
		}
		//console.log(TomcatUrl)
		mui('#UL').on('tap','.mui-icon-phone',dialPhone)
		mui('#UL .photo img').each(function (index,element) {
			element.addEventListener('error',photoLoadError)
		})
	}
	function dialPhone(){
		var phoneStr = this.parentNode.querySelector('.phone').innerText
		if (phoneStr == '暂无') {
			mui.toast('无电话号码')
			return
		}else if (phoneStr.length < 5) {
			mui.toast('号码不正确')
			return
		}
		plus.device.dial(phoneStr, true);
		
	}
	function photoLoadError(){
		//console.log(this.src)
		var photoDom = this.parentNode
		photoDom.innerHTML = photoDom.getAttribute('data-name')
		photoDom.removeAttribute('style')
	}
	function showNullTip(msg){
		mui('.nullData')[0].style.display = 'block';
		mui('.nullTip')[0].innerHTML = msg
	}
	
	function hideNullTip(){
		mui('.nullData')[0].style.display = 'none';
	}
	
	function getAllWards(){
		var pdict = {'nurseid':nurseid}
		var paramDict = {'method':'web.INMDataLimit:FindRoleWardList','params':JSON.stringify(pdict),'type':'RecQuery'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var wardArr = result['data']
			if (!wardArr || wardArr.length == 0) {
				showNullTip('暂无数据')
			}else{
				wardID = wardArr[0].rw
				wardDesc = wardArr[0].WardDesc
				getListData()
			}
		}, function(errorStr) {
			
		});
	}
})(mui)